package com.wyf.progressbartest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.wyf.progressbartest.widget.RoundProgressBar;
import com.wyf.progressbartest.widget.VerticalProgressBar;

public class ProgressBarTestActivity extends Activity implements
		OnClickListener {

	private RoundProgressBar mRoundProgressBar1, mRoundProgressBar2,
			mRoundProgressBar3, mRoundProgressBar4;
	private VerticalProgressBar pb_vertical_custom;
	private ProgressBar pb_horizontal_custom, pb_google_styled;
	private Button btn_go, btn_reset;
	private int progress = 0;
	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_progress_bar_test);
		initView();
	}

	private void initView() {
		mRoundProgressBar1 = (RoundProgressBar) findViewById(R.id.roundProgressBar1);
		mRoundProgressBar2 = (RoundProgressBar) findViewById(R.id.roundProgressBar2);
		mRoundProgressBar3 = (RoundProgressBar) findViewById(R.id.roundProgressBar3);
		mRoundProgressBar4 = (RoundProgressBar) findViewById(R.id.roundProgressBar4);
		pb_vertical_custom = (VerticalProgressBar) findViewById(R.id.pb_vertical_custom);
		pb_horizontal_custom = (ProgressBar) findViewById(R.id.pb_horizontal_custom);
		pb_google_styled = (ProgressBar) findViewById(R.id.pb_google_styled);
//		mRoundProgressBar1.setMax(200);
		HandlerThread handlerThread = new HandlerThread("netWork");
		handlerThread.start();
		handler = new Handler(handlerThread.getLooper());

		btn_go = (Button) findViewById(R.id.btn_go);
		btn_reset = (Button) findViewById(R.id.btn_reset);

		// 设置进度监听器
		mRoundProgressBar1
				.setOnLoadFinishListener(new RoundProgressBar.OnLoadFinishListener() {

					@Override
					public void onLoadFinished() {
						Toast.makeText(getApplicationContext(), "进度条加载完成事件触发了",
								Toast.LENGTH_LONG).show();
					}
				});

		btn_go.setOnClickListener(this);
		btn_reset.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_go:
			new Thread(new Runnable() {

				@Override
				public void run() {
					handler.post(new Runnable() {

						@Override
						public void run() {
						}
					});
					while (progress <= 100) {
						progress += 3;
						mRoundProgressBar1.setProgress(progress);
						mRoundProgressBar2.setProgress(progress);
						mRoundProgressBar3.setProgress(progress);
						mRoundProgressBar4.setProgress(progress);
						pb_vertical_custom.setProgress(progress);
						pb_horizontal_custom.setProgress(progress);
						pb_google_styled.setProgress(progress);
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}

				}
			}).start();
			break;
		case R.id.btn_reset:
			progress = 0;
			mRoundProgressBar1.setProgress(progress);
			mRoundProgressBar2.setProgress(progress);
			mRoundProgressBar3.setProgress(progress);
			mRoundProgressBar4.setProgress(progress);
			pb_vertical_custom.setProgress(progress);
			pb_horizontal_custom.setProgress(progress);
			pb_google_styled.setProgress(progress);
			break;
		default:
			break;
		}
	}
}
